from Model import Model as gameModel
from Cutscenes.Model import Model as csModel;
from StandardView import StandardView
from Cutscenes.CutsceneView import CutsceneView;
from GameWindow import GameWindow;
from Cutscenes.LongStrings import LongStrings
from Cutscenes.SceneDescription import SceneDescription;
from pygame.mixer import music

def parseConfigFile():
    attributes = dict()
    configFile=open('configFile.txt')
    for configLine in configFile:
        configLineParts=configLine.strip().split('=')
        attributes[configLineParts[0]]=configLineParts[1]
    return(attributes)

def parseConductorFile():
    
    conductFile = open(attributes['conductorFile'])
    conduct = []
    for line in conductFile:
        if not line[0]=="#":
            splitline = line.strip().split("|")
            if len(splitline)<2:
                raise Exception("invalid line: "+line)
            conduct.append((splitline[0],splitline[1]))
    return conduct;

if __name__ == '__main__':
    attributes = parseConfigFile()
    conduct = parseConductorFile()
    strings = LongStrings(attributes['longTextFile'])
    window = GameWindow(attributes['screenWidth'],attributes['screenHeight'],"City of Steel")
    music.load(attributes['bgMusicFile'])
    music.set_volume(float(attributes['bgMusicVolume']))
    music.play(-1)
    for action in conduct:
        model = None;
        view = None;
        if action[0] == "level":
            model = gameModel(attributes['mapFolder'],'\\'+action[1],attributes['unitListFolder'],attributes['weaponListFolder'],attributes['squareSize'])
            view = StandardView(attributes['imageFolder'],window, attributes['squareSize'],attributes['cursorColor'])
        if action[0] == "scene":
            sd = SceneDescription(attributes['scenesFolder']+"\\"+action[1], strings,attributes['sceneDefaultTime'])
            model = csModel(sd)
            view = CutsceneView(attributes['scenesFolder']+"\\"+sd.folder,sd.background,window);
        model.attach(view);
        model.run();
        model.detach(view);
        while action[0] == "level" and model.winner not in map(lambda s:int(s),attributes['playerTeam'].split(",")):#reinitialize and repeat if failed
            sd = SceneDescription(attributes['scenesFolder']+"\\failure.scene",strings,attributes['sceneDefaultTime'])
            model=csModel(sd)
            view= CutsceneView(attributes['scenesFolder']+"\\"+sd.folder,sd.background,window)
            model.attach(view);
            model.run();
            model.detach(view);
            model = gameModel(attributes['mapFolder'],'\\'+action[1],attributes['unitListFolder'],attributes['weaponListFolder'],attributes['squareSize'])
            view = StandardView(attributes['imageFolder'],window, attributes['squareSize'],attributes['cursorColor'])
            model.attach(view);
            model.run();
            model.detach(view);
            
        
